<?php
include 'config.php';

session_start();

if (!session_is_registered("myusername"))
{
	echo "Please log out and log back in.";
}

?>
	        <form id="faultyModemForm" action="" method="GET">
                <table>
                        <tr><td>Serial Number:</td><td><input class="input" onKeyUp="modemWarranty();" id="modemSerial" name="serial"></input><span id="modemWarranty"></span></td></tr>
	                <tr><td valign="top">Problem Description:</td><td><textarea id="problemDescription" rows="30" cols="50"></textarea></td></tr>        
			<tr><td></td><td><input type="button" id="modemSubmit" value="Send!" onClick="MochaUI.notification('Please Wait...')"/><input type="reset" value="cancel"/></td></tr>

                </table>
		</form>
<script type="text/javascript">
function modemWarranty() {
	var curDate = new Date();
        var month = new Array(12);
	month[1] = "January";
	month[2] = "February";
	month[3] = "March";
	month[4] = "April";
	month[5] = "May";
	month[6] = "June";
	month[7] = "July";
	month[8] = "August";
	month[9] = "September";
	month[10] = "October";
	month[11] = "November";
	month[12] = "December";
	var serial = document.getElementById('modemSerial').value;
        var model = serial.substr(3,3);
	var manfYear = serial.substr(0,2);
	var manfMonth = serial.substr(2,1);
        //var manfDate = "January 1, 200"+serial.substr(6,1);
	if (manfMonth.toUpperCase() == "A") {
		manfMonth = 10;
	} else if (manfMonth.toUpperCase() == "B") {
		manfMonth = 11;
	} else if (manfMonth.toUpperCase() == "C") {
		manfMonth = 12;
	}
	var manfDate = month[manfMonth]+' 1, 20'+manfYear;
	var manfYearMS = Date.parse(manfDate);
        var yearMS = 31536000000;
        var warrantyPeriod = yearMS * 2;//How long is the device warrantied for?
        var manfTotalMS = manfYearMS + warrantyPeriod;

	//  Will add additional models here as time goes on
	//  Could also remove models as time goes on	
	model = model.toUpperCase();
	if (model == "URQ" || model == "URK") {
		model = "CT-5621T";
	} else if (model == "UXK") {
		model = "CT-5071T";
	} else if (model == "UMZ") {
		model = "CT-5071S";
	}

                if (model != "") {
                        if (manfTotalMS > Date.parse(curDate)) {
                                document.getElementById('modemWarranty').innerHTML = "Device is in warranty"; 
                                document.getElementById('modemWarranty').style.color = "green";
                               	if (serial.length == 11) {
					 //  Add Device to DB
	        			$('modemSubmit').addEvent('click', function(e){
						MochaUI.notification('Device Submitted');
						//alert('In Warranty: '+model+' '+manfDate+' ');
                				var url = 'pages/query.php?';
						url=url+'serial='+$('modemSerial').value;
						url=url+'&model='+model;
						url=url+'&problemDescription='+$('problemDescription').value;
						url=url+'&cmd=faultyModem';
						url=url+'&nocache='+Math.random();
						MochaUI.updateContent({
							'element': $('panel1'),
							'loadMethod': 'xhr',
							'url': url,
							'title': 'Command Response',
							'padding': { top: 8, right: 8, bottom: 8, left: 8 }
						});
        					showSpinner();
					});
                        	}
				else if (serial.length < 11) {
					//Device serial is too short
					document.getElementById('modemWarranty').innerHTML = "Device is in warranty, but serial is too short";
					document.getElementById('modemWarranty').style.color = "orange";
				}
				else if (serial.length > 11) {
					document.getElementById('modemWarranty').innerHTML = "Device is in warranty, but serial is too long";
					document.getElementById('modemWarranty').style.color = "orange";
				}
			}
                        else {
                                document.getElementById('modemWarranty').innerHTML = "Device is out of warranty"; 
                                document.getElementById('modemWarranty').style.color = "red";
                        }
                }
                else {
                        document.getElementById('modemWarranty').innerHTML = "Invalid Serial";
                        document.getElementById('modemWarranty').style.color = "red";
                }       
        
        //alert("Current Date: "+Date.parse(curDate)+"\n"+"Manf Date: "+manfTotalMS+"\nModel: "+model+"\nWeek: "+manfWeek);*/
        
}


</script>
